package com.ithema.controller;

import com.itheima.domain.Book;
import com.ithema.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



//http://localhost:8080/maven_02_ssm_war_exploded/pages/books.html
@RestController
@RequestMapping("/books1")
public class BookController_Result {
    @Autowired
    private BookService bookService;

//    查全部
    @GetMapping
    public Result Books_All() {

        List<Book> books = bookService.selectBookAll();
        System.out.println(books);
        return new Result(books!=null?Code.GET_OK:Code.GET_ERROR,books,books!=null?"":"查询失败");
    }

//    查单个
    @GetMapping("/{id}")
    public Result Books_ById(@PathVariable("id") int bookId) {
        Book book = bookService.selectBook_Id(bookId);
        System.out.println(book);
        return new Result(book!=null?Code.GET_OK:Code.GET_ERROR,book,book!=null?"":"查询id失败");
    }

//    保存
    @PostMapping
    public Result Book_save(@RequestBody Book book) {
        boolean b = bookService.saveBook(book);
        return new Result(b?Code.SAVE_OK:Code.SAVE_ERROR,b);
    }

//    根据ID删除
    @DeleteMapping("/{id}")
    public Result Book_delete(@PathVariable("id") Integer id){
        boolean b = bookService.deleteBook(id);
        return new Result(b?Code.DELETE_OK:Code.UPDATE_ERROR,b);
    }

//    修改值
    @PutMapping()
    public Result Book_update(@RequestBody Book book){
        boolean update = bookService.update(book);
        return new Result(update==true?Code.UPDATE_OK:Code.UPDATE_ERROR,update);
    }
}
